import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.Source;
import javax.xml.transform.sax.SAXSource;
import java.io.StringReader;

public class AppMain {

    public static void main(String[] args) throws ParserConfigurationException, org.xml.sax.SAXException, JAXBException {
        DdhObject ddhObject = new DdhObject();
        Airline airline = new Airline("1", "2", "2", "update");
        ddhObject.setXmlns("https://aaa");
        ddhObject.setAirline(airline);
        String str = XMLUtils.convertToXml(ddhObject);

        System.out.println(str);
        Source source = trunSource(str);
        JAXBContext context = JAXBContext.newInstance(DdhObject.class);
        // 进行将Xml转成对象的核心接口
        Unmarshaller unmarshaller = context.createUnmarshaller();
        DdhObject unmarshal = (DdhObject) unmarshaller.unmarshal(source);
        System.out.println(unmarshal.toString());
    }

    private static Source trunSource(String xmlStr) throws org.xml.sax.SAXException, ParserConfigurationException {
        StringReader reader = new StringReader(xmlStr);
        SAXParserFactory sax = SAXParserFactory.newInstance();
        sax.setNamespaceAware(false);
        XMLReader xmlReader = sax.newSAXParser().getXMLReader();
        Source source = new SAXSource(xmlReader, new InputSource(reader));
        return source;
    }
}
